/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2023 AUTHOR,AFFILIATION
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application 
    matrixDumpFoam

Description
    Dump OpenFOAM matrix test 

    @author tttt
    @date  2023-11
    @version OpenFOAM-v2306

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"
#include "simpleControl.H"
#include "OFstream.H"
#include "simpleMatrix.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    #include "postProcess.H"
    #include "addCheckCaseOptions.H"
    #include "setRootCaseLists.H"
    #include "createTime.H"
    #include "createMesh.H"

    simpleControl simple(mesh);

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    
    // 1. 注册场（volScalarField + volVecotField)
    // 2. 准备一些用于写矩阵系数的文件
    // 3. 组装矩阵
        // 3.1 OpenFOAM组装
        // 3.2 自己手动组装
    // 4. 组装后的矩阵写入文件 
    // 5. 与解析解一维导热案例对比
    // 6. 与gdbOF手册内案例对比
    #include "createFields.H"
    Info<< "\nCalculating 1-D temperature distribution with analytic solution\n" << endl;
    while (simple.loop())
    {
        while (simple.correctNonOrthogonal())
        {
            // OpenFOAM的矩阵LDU存储
            fvScalarMatrix TEqn
            (
                fvm::laplacian(DT, T)
            );
            TEqn.solve();//这句话后：while (simple.correctNonOrthogonal())，TEqn被释放
        

        // 定义用于保存结果的文件
        fileName outputDir(mesh.time().path()/"tttt");
        mkDir(outputDir);

        autoPtr<OFstream> outputFilePtr;
        outputFilePtr.reset(new OFstream(outputDir/"matrix_dump.txt"));

        outputFilePtr() << "Time: " << runTime.timeName() << endl;
        outputFilePtr() << "Matrix dump: " << endl;

        //打印及输出求解信息
        forAll(T,cellI)
        {
            Info << "OF's solve() at cell: " << cellI << " : " << mesh.C()[cellI].component(vector::X)
                 << " with value: "<<T[cellI] << " "
                 << endl;
            outputFilePtr() << "OF's solve() at cell: " << cellI << " : " << mesh.C()[cellI].component(vector::X)
                 << " with value: "<<T[cellI] << " "
                 << endl;
        }

        #include "assembleMatrix.H"
        }

    }

    if(runTime.writeTime())
    {
       runTime.write();
    }

    Info<< nl;
    runTime.printExecutionTime(Info);

    Info<< "End\n" << endl;

    return 0;
}


// ************************************************************************* //
